<?php

namespace Biz\service;

use Biz\cache\SkuAttr as cSkuAttr;
use Biz\cache\SkuBase as cSkuBase;
use Biz\cache\SkuGoodsMap as cSkuGoodsMap;
use Biz\model\GoodsSku as mSkuInfo;
use Biz\model\GoodsSkuAttr as mSkuAttr;
use Biz\module\Packages\GoodsFormatInfo\Scene\Goods as fGoods;
use Biz\module\Packages\GoodsFormatInfo\Scene\Sku as fSku;
use Biz\pockets\constInterface\SkuFields;
use Biz\service\activity\PinTuan;
use Doraemon\model\Category as mCategory;
use Biz\service\activity\AllActivityInfo as sAllActivityInfo;
use Biz\service\Goods as sGoods;
use Biz\service\Stock as sStock;
use Doraemon\pockets\publicTrait\CheckConstants;
use Biz\pockets\constInterface\ActivityInfo;
use Biz\service\activity\AllactivityInfoBak;
/**
 * @deprecated
 */
class Sku implements SkuFields
{
    use CheckConstants;

    /**
     * 获取订单所需的sku信息
     * 默认按照优先级获取数据,如果有渠道的话,传递渠道的数据
     * @param array $skuIds skuids
     * @param mixed $activityInfo ['activity_id' => 活动id ,'type' => 活动类型]
     * @return array
     * !!!! warning 只有下单的地方会调用该方法
     */
    public static function Sku4Order($skuIds, $activityInfo = [])
    {
        if(empty($skuIds)) {
            return [];
        }
        if(empty($activityInfo)) {
            $activityInfo['not_activity_type'] = [ActivityInfo::ACTIVITY_TYPE_PINTUAN];
        }
        list($cacheData, $noCacheKeys) = cSkuGoodsMap::hMGetItems($skuIds);
        if ($noCacheKeys) {
            $skuGoodsMaps = mSkuInfo::getSkuGoodsMap($noCacheKeys);
            cSkuGoodsMap::hMSetItems($skuGoodsMaps);
            $skuGoodsMaps = $cacheData + $skuGoodsMaps;
        } else {
            $skuGoodsMaps = $cacheData;
        }
        $goodsSkuBaseMaps = [];
        foreach ($skuGoodsMaps as $skuId => $goodsId) {
            $goodsSkuBaseMaps[$goodsId][] = $skuId;
        }
        list($skuBases, $noCacheKeys) = cSkuBase::pHMGetItems($goodsSkuBaseMaps);
        if ($noCacheKeys) {
            $skuBases = mSkuInfo::getSkusByGoodsIds(array_keys($noCacheKeys));
            cSkuBase::pHMSetItems($skuBases);
            list($skuBases, $noCacheKeys) = cSkuBase::pHMGetItems($goodsSkuBaseMaps);
        }
        $goodsSkuAttrMaps = [];
        foreach ($skuBases as $goodsId => $skus) {
            foreach ($skus as $sku) {
                foreach (fSku::SKU_ATTR_KEYS as $key => $value) {
                    if ($sku[$key]) {
                        $goodsSkuAttrMaps[$goodsId][] = $sku[$key];
                    }
                }
            }
        }
        list($skuAttrs, $noCacheKeys) = cSkuAttr::pHMGetItems($goodsSkuAttrMaps);
        if ($noCacheKeys) {
            $skuAttrs = mSkuAttr::getSkuAttrsByGoodsIds(array_keys($noCacheKeys));
            cSkuAttr::pHMSetItems($skuAttrs);
            list($skuAttrs, $noCacheKeys) = cSkuAttr::pHMGetItems($goodsSkuAttrMaps);
        }
        //获取sku的基础库存信息
        $skuStocks = sStock::getSotcksByGoodsIds(array_keys($goodsSkuBaseMaps));
        $skuActivityInfos = AllactivityInfoBak::getActivityListBySkuIds($skuIds, $activityInfo);
        $skuGoodsInfo = sGoods::Goods(array_keys($goodsSkuAttrMaps), sGoods::FIELDS_GROUP_ORDER);
        $skuInfos = [];
        foreach ($skuBases as $goodsId => $skus) {
            foreach ($skus as $sku) {
                if(isset($skuGoodsInfo[$sku['goods_id']])) {
                    $skuInfos[$sku['id']] = fSku::formatOrderSku($sku, $skuStocks[$goodsId][$sku['id']],
                        $skuAttrs[$goodsId], isset($skuActivityInfos[$sku['id']]) ? $skuActivityInfos[$sku['id']] : [],
                        $skuGoodsInfo[$sku['goods_id']]);
                }
            }
        }
        foreach ($skuIds as $skuId) {
            $skuInfos[$skuId] = isset($skuInfos[$skuId]) ? $skuInfos[$skuId] : ['sku_id' => $skuId, 'status' => mSkuInfo::STATUS_DELETE];
        }
        return $skuInfos;
    }

    public static function getInfo($goodsIds, $activity_type = null)
    {
        $response = [];

        $activityInfo = [
            'activity_type' => $activity_type ?: ActivityInfo::ACTIVITY_TYPE_NORMAL,
        ];

        //查询sku基本信息
        $list = self::getSkuBasic($goodsIds);

        //查询sku属性信息
        $attr = self::getSkuAttr($goodsIds);

        //获取sku库存
        $stocks = sStock::getStocksByGoodsIds($goodsIds);

        //获取sku活动
        $activity = AllactivityInfoBak::getActivityListByGoodsIds($goodsIds, $activityInfo);

        if (empty($list)) return $response;

        foreach ($goodsIds as $goodsId) {
            $temp = empty($list[$goodsId]) ? [] : $list[$goodsId];
            $tempAttr = empty($attr[$goodsId]) ? [] : $attr[$goodsId];
            $tempStock = empty($stocks[$goodsId]) ? [] : $stocks[$goodsId];
            $tempActivity = empty($activity[$goodsId]) ? [] : $activity[$goodsId][sAllActivityInfo::STARTING_STATUS_KEY];
            if (!$temp) continue;
            $responseItem = [];
            foreach ($temp as $basic) {
                $skuId = $basic['id'] ?? 0;
                $skuActivity = $tempActivity[$skuId] ?? [];
                $responseItem[$skuId] = [
                    'sku_id' => $skuId,
                    'sku_desc' => \Biz\module\Packages\GoodsFormatInfo\Scene\Sku::getAttrStr($basic, $tempAttr),
                    'stock' => $tempStock[$skuId] ?? null,
                    'activity_stock' => $skuActivity['actual_stock'] ?? null,
                    'price' => $basic['price'] ?? null,
                    'activity_price' => $skuActivity['activity_price'] ?? null,
                ];
            }
            $response[$goodsId] = $responseItem;
        }

        return $response;
    }

    /**
     * 查询sku属性
     * @param $goodsIds
     * @return array
     */
    public static function getSkuAttr($goodsIds)
    {
        $skuAttrs = [];

        if (empty($goodsIds)) return $skuAttrs;

        list($cacheData, $noCacheKeys) = cSkuAttr::pHGetAllItems($goodsIds);
        if ($noCacheKeys) {
            $skuAttrs = mSkuAttr::getSkuAttrsByGoodsIds($noCacheKeys);
            cSkuAttr::pHMSetItems($skuAttrs);
            $skuAttrs = $cacheData + $skuAttrs;
        } else {
            $skuAttrs = $cacheData;
        }

        return $skuAttrs;
    }

    /**
     * 查询sku基础信息
     * @param $goodsIds
     * @return array
     */
    public static function getSkuBasic($goodsIds)
    {
        $skuBases = [];

        if (empty($goodsIds)) return $skuBases;

        //获取sku的基础价格信息
        list($cacheData, $noCacheKeys) = cSkuBase::pHGetAllItems($goodsIds);
        if ($noCacheKeys) {
            $skuBases = mSkuInfo::getSkusByGoodsIds($noCacheKeys);
            cSkuBase::pHMSetItems($skuBases);
            $skuBases = $cacheData + $skuBases;
        } else {
            $skuBases = $cacheData;
        }

        return $skuBases;
    }

    public static function Sku($goodsIds, $fieldsGroup = self::FIELDS_GROUP_BASE, $activityInfo = [])
    {
        $activityInfo['activity_type'] = isset($activityInfo['activity_type']) ? $activityInfo['activity_type'] : ActivityInfo::ACTIVITY_TYPE_NORMAL;
        $activityInfo['redundancy_type'] = isset($activityInfo['redundancy_type']) ? $activityInfo['redundancy_type'] : 0;
        $activityInfo['not_activity_type'] = isset($activityInfo['not_activity_type']) ? $activityInfo['not_activity_type'] : [];

        $params = self::verfyParams($goodsIds, $fieldsGroup);

        switch ($fieldsGroup) {
            case self::FIELDS_GROUP_ALL:
            case self::FIELDS_GROUP_GOODS_DETAIL:
                list($cacheData, $noCacheKeys) = cSkuAttr::pHGetAllItems($params['goods_ids']);
                if ($noCacheKeys) {
                    $skuAttrs = mSkuAttr::getSkuAttrsByGoodsIds($noCacheKeys);
                    cSkuAttr::pHMSetItems($skuAttrs);
                    $skuAttrs = $cacheData + $skuAttrs;
                } else {
                    $skuAttrs = $cacheData;
                }
            case self::FIELDS_GROUP_BASE:
            case self::FIELDS_GROUP_SKU_INFO:
                //获取sku的基础价格信息
                list($cacheData, $noCacheKeys) = cSkuBase::pHGetAllItems($params['goods_ids']);
                $noCacheKeys = $params['goods_ids'];
                if ($noCacheKeys) {
                    $skuBases = mSkuInfo::getSkusByGoodsIds($noCacheKeys);
                    cSkuBase::pHMSetItems($skuBases);
                    $skuBases = $cacheData + $skuBases;
                } else {
                    $skuBases = $cacheData;
                }
                //获取sku的基础库存信息
                $skuStocks = sStock::getSotcksByGoodsIds($params['goods_ids']);
                //获取sku的活动信息（价格 库存）
                $skuActivityInfos = AllactivityInfoBak::getActivityListByGoodsIds($params['goods_ids'], $activityInfo);
        }

        $skuInfos = [];
        foreach ($skuBases as $goodsId => $skus) {
            switch ($fieldsGroup) {
                case self::FIELDS_GROUP_ALL:
                    $skuInfos[$goodsId] = fSku::formatSku($skus, $skuStocks[$goodsId], $skuAttrs[$goodsId],
                        isset($skuActivityInfos[$goodsId]) ? $skuActivityInfos[$goodsId] : []);
                case self::FIELDS_GROUP_GOODS_DETAIL:
                    $skuInfos[$goodsId]['base']['size_spec_params'] = fSku::formatSkuSpecParams($skus,
                        $skuAttrs[$goodsId]);
                case self::FIELDS_GROUP_BASE:
                    $skuInfos[$goodsId]['base'] = array_merge($skuInfos[$goodsId]['base'],
                        self::formatPriceAndStock($skus, $skuStocks[$goodsId],
                            isset($skuActivityInfos[$goodsId]) ? $skuActivityInfos[$goodsId] : []));
                    break;
                case self::FIELDS_GROUP_SKU_INFO:
                    $skuInfos[$goodsId] = fSku::formatSkuInfo($skus, $skuStocks[$goodsId],
                        isset($skuActivityInfos[$goodsId]) ? $skuActivityInfos[$goodsId] : []);
            }
        }
        if ($params['single']) {
            switch ($fieldsGroup) {
                case self::FIELDS_GROUP_ALL:
                    $goodsBaseInfo = sGoods::getGoodsBasicInfos($params['goods_ids']);
                    $skuInfos = fGoods::formatGoods4Sku($goodsBaseInfo, $skuInfos);
                    return $skuInfos[$goodsIds];
                case self::FIELDS_GROUP_BASE:
                case self::FIELDS_GROUP_GOODS_DETAIL:
                    return $skuInfos[$goodsIds]['base'];
                    break;
                case self::FIELDS_GROUP_SKU_INFO:
                    return $skuInfos[$goodsIds];
            }
        } else {
            switch ($fieldsGroup) {
                case self::FIELDS_GROUP_ALL:
                    return $skuInfos;
                    break;
                case self::FIELDS_GROUP_BASE:
                case self::FIELDS_GROUP_GOODS_DETAIL:
                    return array_map(function ($skuInfo) {
                        return $skuInfo['base'];
                    }, $skuInfos);
                    break;
                case self::FIELDS_GROUP_SKU_INFO:
                    return $skuInfos;
            }
        }
    }

    public static function skuList($goodsId)
    {
        $skuList = self::Sku($goodsId, self::FIELDS_GROUP_ALL, ['activity_type' => ActivityInfo::ACTIVITY_TYPE_PINTUAN]);
        $pinTuan = PinTuan::getCurrentList([$goodsId]);
        $skuAttrIds = [];
        $prices = $oriPrices = [];
        if (isset($pinTuan[$goodsId]) && $pinTuan[$goodsId]['skus']) {
            $skuIds = array_column($pinTuan[$goodsId]['skus'], 'sku_id');
            $pinTuan[$goodsId]['skus'] = array_column($pinTuan[$goodsId]['skus'], null, 'sku_id');
            foreach ($skuList['sku_info'] as $k => $skuInfo) {
                $skuList['sku_info'][$k]['is_selected'] = false;
                if (!in_array($skuInfo['sku_id'], $skuIds)) {
                   unset($skuList['sku_info'][$k]);
                } else {
                    $prices[] = $pinTuan[$goodsId]['skus'][$skuInfo['sku_id']]['activity_price'];
                    $oriPrices[] = $skuList['sku_info'][$k]['ori_price'];
                    $skuAttrIds[] = isset($skuInfo['sku_params']['t1']) ? $skuInfo['sku_params']['t1'] : 0;
                    $skuAttrIds[] = isset($skuInfo['sku_params']['t2']) ? $skuInfo['sku_params']['t2'] : 0;
                }
            }

            if ($prices && $skuList['sku_info']) {
                $skuList['goods_info']['ori_min_price'] = min($oriPrices);
                $skuList['goods_info']['ori_max_price'] = max($oriPrices);
                $skuList['goods_info']['actual_max_price'] = max($prices);
                $skuList['goods_info']['actual_min_price'] = min($prices);
                $skuList['sku_info'] = array_values($skuList['sku_info']);
                $skuList['sku_info'][0]['is_selected'] = true;
            }
        }

        $skuAttrIds = array_filter($skuAttrIds);
        if ($skuAttrIds) foreach ($skuList['sku_attr'] as $t => &$skuAttr) {
            foreach ($skuAttr['params'] as $k => $param) {
               if(!in_array($param['id'], $skuAttrIds)) {
                   unset($skuAttr['params'][$k]);
               }
            }
            $skuAttr['params'] = array_values($skuAttr['params']);
        }
        return $skuList;
    }

    private static function verfyParams($goodsIds, $fieldsGroup)
    {
        $verfyResult = [];
        if (is_array($goodsIds)) {
            $verfyResult['goods_ids'] = array_filter($goodsIds, function ($goodsId) {
                return is_numeric($goodsId);
            });
            $verfyResult['single'] = false;
        } elseif (is_numeric($goodsIds)) {
            $verfyResult['goods_ids'] = [$goodsIds];
            $verfyResult['single'] = true;
        } else {
            throw new \Exception("请求参数异常");
        }
        if (!self::checkConstants(self::_CONST_FIELDS_GROUP_PREFIX, $fieldsGroup)) {
            throw new \Exception("请求参数异常");
        } else {
            $verfyResult['fields_group'] = $fieldsGroup;
        }
        return $verfyResult;
    }

    private static function formatPriceAndStock($skus, $skuStocks, $skuActivityInfos)
    {
        $oriMinPrice = null;//原价最低价
        $oriMaxPrice = null;//原价最高价
        $oriMinPriceWithStock = null;//原价最低价（有库存）
        $oriMaxPriceWithStock = null;//原价最高价（有库存）
        $actualMinPrice = null;//当前价最低价
        $actualMaxPrice = null;//当前价最高价
        $actualMinPriceWithStock = null;//当前价最低价（有库存）
        $actualMaxPriceWithStock = null;//当前价最高价（有库存）
        $actualStock = 0;//实际库存
        $activityStock = 0;//活动库存
        $activityType = null;
        $activityId = null;
        $activityStartTime = null;
        $activityEndTime = null;
        $activityLimitBuyCount = null;
        $previewActivityId = null;
        $previewActivityType = null;
        $previewActivityTime = null;
        $activityPreviewTime = null;
        $previewActivityStartTime = null;
        $previewActivityEndTime = null;
        $previewMinPrice = null;
        $previewMaxPrice = null;
        $activityMinPrice = null;
        $activityMaxPrice = null;
        $redundancyMinPrice = null; // 冗余最低价
        $redundancyMaxPrice = null; // 冗余最高价
        $redundancyInfo = []; // 冗余信息

        foreach ($skus as $skuId => $sku) {
            $oriMinPrice = is_null($oriMinPrice) ? $sku['price'] : min($oriMinPrice, $sku['price']);
            $oriMaxPrice = is_null($oriMaxPrice) ? $sku['price'] : max($oriMaxPrice, $sku['price']);
            if ($skuActivityInfos && isset($skuActivityInfos[sAllActivityInfo::STARTING_STATUS_KEY]) && isset($skuActivityInfos[sAllActivityInfo::STARTING_STATUS_KEY][$skuId])) {
                $skuActivityInfo = $skuActivityInfos[sAllActivityInfo::STARTING_STATUS_KEY][$skuId];
                $stock = isset($skuActivityInfo['actual_stock']) ? $skuActivityInfo['actual_stock'] : $skuStocks[$skuId];
                $oriMinPriceWithStock = ($skuStocks[$skuId] > 0 || $stock > 0) ? (is_null($oriMinPriceWithStock) ? $sku['price'] : min($oriMinPriceWithStock,
                    $sku['price'])) : $oriMinPriceWithStock;
                $oriMaxPriceWithStock = ($skuStocks[$skuId] > 0 || $stock > 0) ? (is_null($oriMaxPriceWithStock) ? $sku['price'] : max($oriMaxPriceWithStock,
                    $sku['price'])) : $oriMaxPriceWithStock;
                $actualMinPrice = $activityMinPrice = is_null($actualMinPrice) ? $skuActivityInfo['activity_price'] : min($actualMinPrice,
                    $skuActivityInfo['activity_price']);
                $actualMaxPrice = $activityMaxPrice =is_null($actualMaxPrice) ? $skuActivityInfo['activity_price'] : max($actualMaxPrice,
                    $skuActivityInfo['activity_price']);
                $actualMinPriceWithStock = is_null($actualMinPriceWithStock) ? ($stock > 0 ? $skuActivityInfo['activity_price'] : ($skuStocks[$skuId] > 0 ? $sku['price'] : $actualMinPriceWithStock)) : min($actualMinPriceWithStock,
                    ($stock > 0 ? $skuActivityInfo['activity_price'] : ($skuStocks[$skuId] > 0 ? $sku['price'] : $actualMaxPriceWithStock)));
                $actualMaxPriceWithStock = is_null($actualMaxPriceWithStock) ? ($stock > 0 ? $skuActivityInfo['activity_price'] : ($skuStocks[$skuId] > 0 ? $sku['price'] : $actualMaxPriceWithStock)) : max($actualMaxPriceWithStock,
                    ($stock > 0 ? $skuActivityInfo['activity_price'] : ($skuStocks[$skuId] > 0 ? $sku['price'] : $actualMaxPriceWithStock)));
                $actualStock += $stock;
                $activityStock += $stock;
                $activityType = $activityType ?: $skuActivityInfo['type'];
                $activityId = $activityId ?: $skuActivityInfo['activity_id'];
                $activityPreviewTime = $previewActivityTime ?: $skuActivityInfo['preview_start_time'];
                $activityStartTime = $activityStartTime ?: $skuActivityInfo['start_time'];
                $activityEndTime = $activityEndTime ?: $skuActivityInfo['end_time'];
                $activityLimitBuyCount = isset($skuActivityInfo['limit_buy_count']) ? $activityLimitBuyCount?: $skuActivityInfo['limit_buy_count'] : null;

            } else {
                if ($skuActivityInfos && isset($skuActivityInfos[sAllActivityInfo::PREVIEW_STATUS_KEY]) && isset($skuActivityInfos[sAllActivityInfo::PREVIEW_STATUS_KEY][$skuId])) {
                    $skuActivityInfo = $skuActivityInfos[sAllActivityInfo::PREVIEW_STATUS_KEY][$skuId];

                    $previewActivityType = $previewActivityType ?: $skuActivityInfo['type'];
                    $previewActivityId = $previewActivityId ?: $skuActivityInfo['activity_id'];
                    $previewActivityStartTime = $previewActivityStartTime ?: $skuActivityInfo['start_time'];
                    $previewActivityEndTime = $previewActivityEndTime ?: $skuActivityInfo['end_time'];
                    $previewActivityTime = $previewActivityTime ?: $previewActivityTime['preview_start_time'];
                    $activityStock += isset($skuActivityInfo['actual_stock']) ? $skuActivityInfo['actual_stock'] : $skuStocks[$skuId];
                    $activityLimitBuyCount = isset($skuActivityInfo['limit_buy_count']) ? $activityLimitBuyCount?: $skuActivityInfo['limit_buy_count'] : null;
                }
                $oriMinPriceWithStock = (isset($skuStocks[$skuId]) && $skuStocks[$skuId] > 0) ? (is_null($oriMinPriceWithStock) ? $sku['price'] : min($oriMinPriceWithStock,
                    $sku['price'])) : $oriMinPriceWithStock;
                $oriMaxPriceWithStock = (isset($skuStocks[$skuId]) && $skuStocks[$skuId] > 0) ? (is_null($oriMaxPriceWithStock) ? $sku['price'] : max($oriMaxPriceWithStock,
                    $sku['price'])) : $oriMaxPriceWithStock;
                $actualMinPrice = is_null($actualMinPrice) ? $sku['price'] : min($actualMinPrice,
                    $sku['price']);
                $actualMaxPrice = is_null($actualMaxPrice) ? $sku['price'] : max($actualMaxPrice,
                    $sku['price']);
                $actualMinPriceWithStock = (isset($skuStocks[$skuId]) && $skuStocks[$skuId]) > 0 ? (is_null($actualMinPriceWithStock) ? $sku['price'] : min($actualMinPriceWithStock,
                    $sku['price'])) : $actualMinPriceWithStock;
                $actualMaxPriceWithStock = (isset($skuStocks[$skuId]) && $skuStocks[$skuId]) > 0 ? (is_null($actualMaxPriceWithStock) ? $sku['price'] : max($actualMaxPriceWithStock,
                    $sku['price'])) : $actualMaxPriceWithStock;;
                    if (isset($skuStocks[$skuId])) {
                        $actualStock += $skuStocks[$skuId];
                    }
            }

            if ($skuActivityInfos && isset($skuActivityInfos[sAllActivityInfo::PREVIEW_STATUS_KEY]) && isset($skuActivityInfos[sAllActivityInfo::PREVIEW_STATUS_KEY][$skuId])) {
                $skuActivityInfo = $skuActivityInfos[sAllActivityInfo::PREVIEW_STATUS_KEY][$skuId];
                if (time() < strtotime($skuActivityInfo['start_time'])) {
                    // 预告相关
                    $previewMinPrice = $previewMinPrice ? min($skuActivityInfo['activity_price'], $previewMinPrice) : $skuActivityInfo['activity_price'];
                    $previewMaxPrice = $previewMaxPrice ? max($skuActivityInfo['activity_price'], $previewMaxPrice) : $skuActivityInfo['activity_price'];
                    $previewActivityId =  $previewActivityId ? : $skuActivityInfo['activity_id'];
                    $previewActivityType = $previewActivityType? : $skuActivityInfo['type'];
                    $previewActivityStartTime = $previewActivityStartTime ?: $skuActivityInfo['start_time'];
                    $previewActivityEndTime = $previewActivityEndTime ?: $skuActivityInfo['end_time'];
                    $previewActivityTime = $previewActivityTime ?: $skuActivityInfo['preview_start_time'];
                }
            }

            if($skuActivityInfos && isset($skuActivityInfos[sAllActivityInfo::REDUNDANCY_KEY]) && isset($skuActivityInfos[sAllActivityInfo::REDUNDANCY_KEY][$skuId])) {
                $skuActivityInfo = $skuActivityInfos[sAllActivityInfo::REDUNDANCY_KEY][$skuId];
                $redundancyMinPrice = is_null($redundancyMinPrice) ? $skuActivityInfo['activity_price'] : min($redundancyMinPrice, $skuActivityInfo['activity_price']);
                $redundancyMaxPrice = is_null($redundancyMinPrice) ? $skuActivityInfo['activity_price'] : max($redundancyMinPrice, $skuActivityInfo['activity_price']);

                    $redundancyInfo = [
                    'activity_id' => $skuActivityInfo['activity_id'],
                    'activity_type' => $skuActivityInfo['type'],
                    'activity_stock' => $skuActivityInfo['activity_stock'],
                        'title' => $skuActivityInfo['title'],
                        'start_time' => $skuActivityInfo['start_time'],
                        'end_time' => $skuActivityInfo['end_time'],
                    'activity_min_price' => $redundancyMinPrice,
                    'activity_max_price' => $redundancyMaxPrice,
                ];
            }

        }

        $result = [
            'ori_min_price' => $actualStock > 0 ? $oriMinPriceWithStock : $oriMinPrice,
            'ori_max_price' => $actualStock > 0 ? $oriMaxPriceWithStock : $oriMaxPrice,
            'actual_min_price' => $actualStock > 0 ? $actualMinPriceWithStock : $actualMinPrice,
            //'actual_max_price' => $actualStock > 0 ? $actualMaxPriceWithStock : $actualMaxPrice,
            'actual_max_price' => $actualMaxPrice,
            'actual_stock' => $actualStock,
            'activity_stock' => $activityStock,
            'activity_id' => $activityId,
            'activity_type' => $activityType,
            'activity_preview_time' => $activityPreviewTime,
            'activity_start_time' => $activityStartTime,
            'activity_end_time' => $activityEndTime,
            'limit_buy_count' => $activityLimitBuyCount,
            'activity_min_price' => $activityMinPrice ,
            'activity_max_price' => $activityMaxPrice,
            'preview_id' => $previewActivityId,
            'preview_type' => $previewActivityType,
            'preview_min_price' => $previewMinPrice,
            'preview_max_price' => $previewMaxPrice,
            'preview_time' => $previewActivityTime,
            'preview_start_time' => $previewActivityStartTime,
            'preview_end_time' => $previewActivityEndTime,
            'redundancy_info' => $redundancyInfo,
            'goods_stock' => $skuStocks
        ];

        if (!$activityMinPrice || !$activityMaxPrice) {
            $result['activity_min_price'] = $result['ori_min_price'];
            $result['activity_max_price'] = $result['ori_max_price'];
        }

        return $result;
    }


    public static function SkuInfos($goodsIds, $fields = self::FIELDS_GROUP_ALL)
    {
        $skuInfos = SkuInfo::getSkuList($goodsIds, [], false);
        $goodsSkuInfos = [];
        foreach ($skuInfos as $skuId => $skuInfo) {
            $goodsSkuInfos[$skuInfo['goods_id']][] = $skuInfo;
        }

        foreach ($goodsSkuInfos as $goodsId => $skuInfos) {
            $is_has_stock = false;
            foreach ($skuInfos as $skuInfo) {
                $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['stock'] = $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['stock'] = $allStock = isset($allStock) ? ($allStock + $skuInfo['stock']) : $skuInfo['stock'];

                $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['min_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['min_price']) ? min(
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['min_price'],
                    $skuInfo['price']) : $skuInfo['price'];
                $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['max_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['max_price']) ? max(
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['max_price'],
                    $skuInfo['price']) : $skuInfo['price'];
                $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['actual_min_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['actual_min_price']) ? min(
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['actual_min_price'],
                    $skuInfo['buy_price']) : $skuInfo['buy_price'];
                $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['actual_max_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['actual_max_price']) ? max(
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId]['actual_max_price'],
                    $skuInfo['buy_price']) : $skuInfo['buy_price'];

                if ((int)$skuInfo['stock']) {
                    $is_has_stock = true;
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['min_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['min_price']) ? min(
                        $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['min_price'],
                        $skuInfo['price']) : $skuInfo['price'];
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['max_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['max_price']) ? max(
                        $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['max_price'],
                        $skuInfo['price']) : $skuInfo['price'];
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['actual_min_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['actual_min_price']) ? min(
                        $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['actual_min_price'],
                        $skuInfo['buy_price']) : $skuInfo['buy_price'];
                    $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['actual_max_price'] = isset($goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['actual_max_price']) ? max(
                        $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId]['actual_max_price'],
                        $skuInfo['buy_price']) : $skuInfo['buy_price'];

                }
            }

            if (!$is_has_stock) {
                $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK][$goodsId] = $goodsSkuInfos[self::FIELDS_GROUP_BASE][$goodsId];
            }
        }

        switch ($fields) {
            case self::FIELDS_GROUP_BASE_WITH_STOCK :
                return $goodsSkuInfos[self::FIELDS_GROUP_BASE_WITH_STOCK];
                break;
            case self::FIELDS_GROUP_BASE :
                return $goodsSkuInfos[self::FIELDS_GROUP_BASE];
                break;
        }

        return $goodsSkuInfos;
    }

    public static function dataStatGoodsFormat($goodsIds)
    {
        $goodsInfo = sGoods::Goods($goodsIds);

        //查询分类名称
        $categoryIds  = array_merge(array_column($goodsInfo, 'category_id'), array_column($goodsInfo, 'second_category_id'));
        $category = mCategory::getCategoryList($categoryIds, null);
        $categoryInfo = empty($category) ? [] : array_column($category, null, 'id');
        foreach ($goodsInfo as $info) {
            $goodsInfo[$info['id']]['category_name'] = isset($categoryInfo[$info['category_id']]) ? $categoryInfo[$info['category_id']]['cate_name'] : '未知分类id:' . $info['category_id'];
            $goodsInfo[$info['id']]['second_category_name'] = isset($categoryInfo[$info['second_category_id']]) ? $categoryInfo[$info['second_category_id']]['cate_name'] : '未知分类id:' . $info['second_category_id'];
        }

        $response = [];
        foreach ($goodsIds as $goodsId) {
            $info = isset($goodsInfo[$goodsId]) ? $goodsInfo[$goodsId] : [];
            $response[$goodsId] = [
                'goods_id' => $goodsId,
                'name' => isset($info['title']) ? $info['title'] : '',
                'category_name' => isset($info['category_name']) ? $info['category_name'] : '',
                'second_category_name' => isset($info['second_category_name']) ? $info['second_category_name'] : '',
            ];
        }
        return $response;
    }

    public static function dataStatSkuFormat($skuIds)
    {
        $skuGoodsMaps = mSkuInfo::getSkuGoodsMap($skuIds);
        $goodsIds = array_values($skuGoodsMaps);
        $goodsInfo = sGoods::Goods($goodsIds);

        //查询分类名称
        $categoryIds  = array_merge(array_column($goodsInfo, 'category_id'), array_column($goodsInfo, 'second_category_id'));
        $categoryInfo = array_column(mCategory::getCategoryList($categoryIds, null), null, 'id');
        foreach ($goodsInfo as $info) {
            $goodsInfo[$info['id']]['category_name'] = isset($categoryInfo[$info['category_id']]) ? $categoryInfo[$info['category_id']]['cate_name'] : '未知分类id:' . $info['category_id'];
            $goodsInfo[$info['id']]['second_category_name'] = isset($categoryInfo[$info['second_category_id']]) ? $categoryInfo[$info['second_category_id']]['cate_name'] : '未知分类id:' . $info['second_category_id'];
        }

        $response = [];
        foreach ($skuIds as $skuId) {
            $goodsId = isset($skuGoodsMaps[$skuId]) ? $skuGoodsMaps[$skuId] : 0;
            $info = isset($goodsInfo[$goodsId]) ? $goodsInfo[$goodsId] : [];
            $response[$skuId] = [
                'sku_id' => $skuId,
                'goods_id' => $goodsId,
                'name' => isset($info['title']) ? $info['title'] : '',
                'category_name' => isset($info['category_name']) ? $info['category_name'] : '',
                'second_category_name' => isset($info['second_category_name']) ? $info['second_category_name'] : '',
            ];
        }
        return $response;
    }
}